/*
    24. Write a program in C to insert a node at the end of a circular linked list. 
    Test Data and Expected Output :

    Input the number of nodes : 3                                                                                
    Input data for node 1 : 2                                                                                    
    Input data for node 2 : 5                                                                                    
    Input data for node 3 : 8                                                                                    
                                                                                                                
    Data entered in the list are :                                                                               
    Data 1 = 2                                                                                                   
    Data 2 = 5                                                                                                   
    Data 3 = 8                                                                                                   
    Input the data to be inserted : 9                                                                            
                                                                                                                
    After insertion the new list are :                                                                           
    Data 1 = 2                                                                                                   
    Data 2 = 5                                                                                                   
    Data 3 = 8                                                                                                   
    Data 4 = 9
*/

#include <stdio.h>
#include <stdlib.h>

typedef struct node *List;
typedef struct node{
    int x;
    List pre;
    List next;
} Node;

List createCircularList(int n)
{
    List newNode = NULL;
    List root = NULL;
    List curNode = NULL;
    int i;
    for(i = 0; i < n; i++){
        newNode = (List)malloc(sizeof(Node));
        if(newNode == NULL){
            printf("Memery is not enough!\n");
            return NULL;
        }
        printf("Input data for node %d : ", i);
        scanf("%d", &(newNode->x));
        if(root == NULL){
            newNode->next = newNode;
            newNode->pre = newNode;
            root = newNode;
            curNode = root;
        }else{
            newNode->pre = curNode;
            newNode->next = root;
            root->pre = newNode;
            curNode->next = newNode;   
            curNode = newNode;        
        }
    }
    return root;
}

List insertNodeAtBegin(List root, int val)
{
    List newNode = (List)malloc(sizeof(Node));
    List curNode = root;
    if(newNode == NULL){
        printf("Memery is not enough!\n");
        return NULL;
    }
    while(curNode->next != root){
        curNode = curNode->next;
    }
    newNode->x = val;
    newNode->next = root;
    newNode->pre = root->pre;
    root->pre = newNode;
    curNode->next = newNode;    
    return newNode;
}

List insertNodeAtEnd(List root, int val)
{
    List newNode = (List)malloc(sizeof(Node));
    List curNode = root;
    if(newNode == NULL){
        printf("Memery is not enough!\n");
        return NULL;
    }
    while(curNode->next != root){
        curNode = curNode->next;
    }
    newNode->x = val;
    newNode->next = root;
    newNode->pre = curNode;
    curNode->next = newNode;
    root->pre = newNode;    
    return root;
}

void printList(List root)
{
    List list = root;
    if(list == NULL){
        printf("The list is empty!\n");
        return;
    }

    while(list != NULL){
        printf("Data = %d\n", list->x);
        if(list->next == root){
            break;
        }
        list = list->next;
    }
}

void printListAddr(List root)
{
    List list = root;
    if(list == NULL){
        printf("The list is empty!\n");
        return;
    }    

    while(list != NULL){
        printf("|%p|%p|%d|%p| <=> ", list, list->pre, list->x, list->next);
        if(list->next == root){
            break;
        }
        list = list->next;
    } 
    printf("\n");       
}

int main(void)
{
    List root = NULL;
    int n, val;
    printf("Input the number of nodes : ");
    scanf("%d", &n);
    root = createCircularList(n);

    printf("\nData entered on the list are :\n");
    printList(root);
    printListAddr(root);

    printf("Input the data to be inserted : ");
    scanf("%d", &val);
    root = insertNodeAtEnd(root, val);
    printf("After insertion the new list are : \n");
    printList(root);
    printListAddr(root);
 
    return 0;
}